package com.powermanager.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.ScrollView;

/**
 * Created by lqy on 2016/5/9.
 */
public class BottomScrollView extends ScrollView {
    private int downX;
    private int downY;
    private int mTouchSlop;
    private OnScrollToBottomListener onScrollToBottom;
    private int lastScrollY;
    private int index = 0;
    private long lastTime=0;
    private long curTime=0;

    public BottomScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
//        setOnTouchListener(this);
    }

    public BottomScrollView(Context context) {
        super(context);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
//        setOnTouchListener(this);
    }



//    @Override
//    protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX,
//                                  boolean clampedY) {
//        super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
//        if(scrollY != 0 && null != onScrollToBottom){
//            onScrollToBottom.onScrollBottomListener(clampedY);
//        }
//    }

    public void setOnScrollToBottomLintener(OnScrollToBottomListener listener){
        onScrollToBottom = listener;
    }

//    @Override
//    public boolean onTouch(View v, MotionEvent event) {
//        switch (event.getAction()) {
//            case MotionEvent.ACTION_DOWN :
//
//                break;
//            case MotionEvent.ACTION_MOVE :
//                index++;
//                break;
//            default :
//                break;
//        }
//        if (event.getAction() == MotionEvent.ACTION_UP &&  index > 0) {
//            index = 0;
//            View view = ((ScrollView) v).getChildAt(0);
//            if (view.getMeasuredHeight() <= v.getScrollY() + v.getHeight()) {
//                //加载数据代码
//                if(null != onScrollToBottom){
//                    onScrollToBottom.onScrollBottomListener(true);
//                }
//            }
//        }
//        return false;
//    }

    public interface OnScrollToBottomListener{
        public void onScrollBottomListener(boolean isBottom);
    }
    @Override
    public boolean onInterceptTouchEvent(MotionEvent e) {
        int action = e.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                downX = (int) e.getRawX();
                downY = (int) e.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:

                int moveY = (int) e.getRawY();
                if (Math.abs(moveY - downY) > mTouchSlop) {
                    return true;
                }
        }
        return super.onInterceptTouchEvent(e);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        // Grab the last child placed in the ScrollView, we need it to determinate the bottom position.
        View view = (View) getChildAt(getChildCount()-1);

        // Calculate the scrolldiff
        int diff = (view.getBottom()-(getHeight()+getScrollY()));

        // if diff is zero, then the bottom has been reached
        if( diff == 0 )
        {
            curTime = System.currentTimeMillis();
            if (curTime-lastTime>100){
                lastTime = curTime;
            if(null != onScrollToBottom) {
                onScrollToBottom.onScrollBottomListener(true);
            }
        }
            // notify that we have reached the bottom
           System.out.println("MyScrollView: Bottom has been reached");
        }

        super.onScrollChanged(l, t, oldl, oldt);
    }
}


